{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": [
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "if not any(path.endswith('textbook') for path in sys.path):\n",
    "    sys.path.append(os.path.abspath('../../..'))\n",
    "from textbook_utils import *"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(ch:wrangling)=\n",
    "# Wrangling Dataframes\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We often need to perform preparatory work on our data before we can begin our\n",
    "analysis. The amount of preparation can vary widely, but there are a few basic steps\n",
    "to move from raw data to data ready for analysis. {numref}`Chapter %s <ch:files>`\n",
    "addressed the initial steps of creating a dataframe from a plain-text\n",
    "source. In this chapter, we assess quality. To do this, we perform validity checks on individual data\n",
    "values and entire columns. In addition to checking the quality of the data, we determine\n",
    "whether or not the data need to be transformed and reshaped to get ready for\n",
    "analysis. Quality checking (and fixing) and transformation are often cyclical:\n",
    "the quality checks point us toward transformations we need to make, and when we\n",
    "check the transformed columns to confirm that our data are ready for analysis,\n",
    "we may discover they need further cleaning."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Depending on the data source, we often have different expectations for quality.\n",
    "Some datasets require extensive wrangling to get them into an analyzable form,\n",
    "and others arrive clean and we can quickly launch into modeling. Here\n",
    "are some examples of data sources and how much wrangling we might expect to do:"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Data from a scientific experiment or study  are typically clean,\n",
    "  are well documented, and have a simple structure. These data are organized to be\n",
    "  broadly shared so that others can build on or reproduce the findings.  They\n",
    "  are typically ready for analysis after little to no wrangling.\n",
    "- Data from government surveys often come with very detailed codebooks and metadata describing how the data are collected and formatted, and these datasets\n",
    "  are also typically ready for exploration and analysis right out of the \"box.\"\n",
    "- Administrative data can be clean, but without inside knowledge of the source,\n",
    "  we may need to extensively check their quality. Also, since we often\n",
    "  use these data for a purpose other than why they were collected in the first place, we\n",
    "  may need to transform features or combine data tables.\n",
    "- Informally collected data, such as data scraped from the web, can be quite\n",
    "  messy and tends to come with little documentation. For example, texts,\n",
    "  tweets, blogs, and Wikipedia tables. usually require formatting and cleaning\n",
    "  to transform them into information ready for analysis.\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this chapter, we break down data wrangling into the following stages: assess data quality; handle missing values; transform features; and reshape the data by modifying its structure and granularity. An important step in assessing the quality of the data is to consider its scope. Data scope was covered in {numref}`Chapter %s <ch:data_scope>`, and we refer you there for a fuller treatment of the topic. "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To clean and prepare data, we also rely on exploratory data analysis, especially visualizations. In this chapter, however, we focus on data wrangling and cover these other, related topics in more detail in {numref}`Chapters %s <ch:eda>` and {numref}`%s <ch:viz>`. "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use the datasets introduced in {numref}`Chapter %s <ch:files>`: the DAWN government survey of emergency room visits related to drug abuse; and the San Francisco administrative data on food safety inspections of restaurants. But we begin by introducing the various data wrangling concepts through another example that is simple enough and clean enough that we can limit our focus in each of the wrangling steps."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
